package com.bwei.like.mynewstop.view;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bwei.like.mynewstop.R;
import com.bwei.like.mynewstop.utils.CommonUtils;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

/**
 * Created by LiKe on 2016/11/1.
 */
public class ViewPageIndicator extends HorizontalScrollView implements View.OnClickListener,  LazyViewPager.OnPageChangeListener {
    private LazyViewPager lazyviewPage;
    private TextView tv_indicator_title;
    private TextView tv_indicator_lin;
    private LinearLayout linearLayout;
    private SlidingMenu slidingMenu;

    public ViewPageIndicator(Context context, AttributeSet attrs) {
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
        super(context, attrs);
=======
        this(context, attrs,0);
>>>>>>> NewsTop
        init();
    }

    private void init() {
    }

    public ViewPageIndicator(Context context) {
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
        super(context);
=======
        this(context,null);
>>>>>>> NewsTop
        init();
    }

    public ViewPageIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    //获取viewpage
    public void setViewPage(LazyViewPager lazyviewPage, SlidingMenu slidingMenu){
        //移除孩子
        this.removeAllViews();
        this.lazyviewPage = lazyviewPage;
        this.slidingMenu = slidingMenu;
        //设置viewpage监听
        lazyviewPage.setOnPageChangeListener(this);
        PagerAdapter adapter = lazyviewPage.getAdapter();

        linearLayout = new LinearLayout(getContext());
        //展示标题
        for (int i = 0; i < adapter.getCount(); i++) {
            View view = CommonUtils.inflter(R.layout.indicator_item);
            view.setOnClickListener(this);
            tv_indicator_title = (TextView) view.findViewById(R.id.tv_indicator_title);
            tv_indicator_lin = (TextView) view.findViewById(R.id.tv_indicator_lin);
            if (i==0){
                tv_indicator_lin.setVisibility(View.VISIBLE);
                tv_indicator_title.setTextColor(Color.RED);
            }else {
                tv_indicator_lin.setVisibility(View.GONE);
                tv_indicator_title.setTextColor(Color.GRAY);
            }
            tv_indicator_title.setText(adapter.getPageTitle(i));
            //添加
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(CommonUtils.dip2px(10),CommonUtils.dip2px(10),CommonUtils.dip2px(10),CommonUtils.dip2px(10));
            linearLayout.addView(view,params);
        }

        ViewPageIndicator.this.addView(linearLayout);
    }


    @Override
    public void onClick(View v) {
        for(int i=0;i<linearLayout.getChildCount();i++){
            View childAt = linearLayout.getChildAt(i);
            if (childAt == v){
                lazyviewPage.setCurrentItem(i);
            }
        }
    }

    /**
     * 页面发生改变的监听
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        for(int i=0;i<linearLayout.getChildCount();i++){
            View childAt = linearLayout.getChildAt(i);
            TextView tv_lin = (TextView) childAt.findViewById(R.id.tv_indicator_lin);
            TextView tv_title = (TextView) childAt.findViewById(R.id.tv_indicator_title);
            if (i == position){
                this.scrollTo((int) (childAt.getX()-(linearLayout.getChildAt(0).getWidth())),0);
                tv_lin.setVisibility(View.VISIBLE);
                tv_title.setTextColor(Color.RED);
            }else {
                tv_lin.setVisibility(View.GONE);
                tv_title.setTextColor(Color.GRAY);
            }
        }
        // 当当天条目是0的时候，设置可以在任意位置拖拽出SlidingMenu
        if (position == 0){
            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        } else {
            // 当在其他位置的时候，设置不可以拖拽出来(SlidingMenu.TOUCHMODE_NONE)，或只有在边缘位置才可以拖拽出来TOUCHMODE_MARGIN
            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        }

    }

    /**
     * 滑动监听
     */
    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
